<?php

namespace App\Service\Message\v1_3_0;

use App\Helpers\Helper;
use App\Model\User;
use App\Model\UserProfile;
use App\Service\BaseService;

/**
 * 今日缘分-推荐主播
 */
class RecHostListService extends BaseService
{
    public function handle()
    {
        $userId = $this->getUserId();
        // return ['list' => []];
        //是否显示弹窗
        $forbidPop = in_array($this->appId, explode(',', env('SHOW_POP_FORBID_APPID', '6')));
        if ($forbidPop || Helper::checkTempExcluded($userId)) return ['list' => []];
        $profile = UserProfile::getProfile($userId);
        if (!$profile || $profile->gender == 0) return ['list' => []];
        //给男用户推荐主播
        $ret['list'] = [];
        $userIds = $this->getHostIds();
        if (!$userIds) return $ret;
        // $userIds = array_slice($userIds, 0, 6);
        $userIds = array_rand(array_flip($userIds), 6);
        $users = UserProfile::getUsersProfile($userIds);
        $imAccounts = User::getImAccounts($userIds);
        array_walk($users, function ($user) use (&$ret, $imAccounts) {
            $item = UserProfile::buildRecItem($user, $imAccounts, 100);
            array_push($ret['list'], $item);
        });
        return $ret;
    }

    function getHostIds()
    {
        $onlineIds = check_dev_env()
            ? UserProfile::getRecHosts(0)
            : UserProfile::getOnlineRecHosts(1);
        shuffle($onlineIds);
        return $onlineIds;
    }
}
